package com.digitaldreamers.ddmonsters;

import org.andengine.engine.Engine;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.atlas.bitmap.BuildableBitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.source.IBitmapTextureAtlasSource;
import org.andengine.opengl.texture.atlas.buildable.builder.BlackPawnTextureAtlasBuilder;
import org.andengine.opengl.texture.atlas.buildable.builder.ITextureAtlasBuilder.TextureAtlasBuilderException;
import org.andengine.opengl.texture.region.ITextureRegion;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.util.adt.color.Color;

import android.content.Context;

import com.digitaldreamers.framework.BaseResourceManager;

public class ResourceManager {
	private static final Object lockObj = new Object();
	private static ResourceManager instance;
	
	private BaseResourceManager mBaseResourceManager;
	private Context mContext;
	private Engine mEngine;
	
	private ResourceManager() {
		mBaseResourceManager = BaseResourceManager.getInstance();
		mContext = mBaseResourceManager.mContext;
		mEngine = mBaseResourceManager.mEngine;
	}
	
	public static ResourceManager getInstance() {
		if(instance == null) {
			synchronized(lockObj) {
				if(instance == null) {
					instance = new ResourceManager();
				}
			}
		}
		return instance;
	}
	
	// ======================== Shared Resources ================= //
	public ITextureRegion mParticlePointTextureRegion;
	public Font mMoonHouseFont;
	
	public void loadSharedResources() {
		loadSharedTextures();
		loadFonts();
	}
	
	public void unloadSharedResources() {
		unloadSharedTextures();
		unloadFonts();
	}
	
	private void loadSharedTextures() {
		String previousAssetBasePath = BitmapTextureAtlasTextureRegionFactory.getAssetBasePath();
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(Constants.FOLDER__SHARED_TEXTURES);
		
		if(mParticlePointTextureRegion == null) {
			BuildableBitmapTextureAtlas textureAtlas = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 480, 480);
			mParticlePointTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas, mContext, "particle_point.png");
			try {
				textureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
				textureAtlas.load();
			} catch (TextureAtlasBuilderException e) {
				
			}
		}
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(previousAssetBasePath);
	}
	
	private void unloadSharedTextures() {
		if(mParticlePointTextureRegion != null) {
			mParticlePointTextureRegion.getTexture().unload();
			mParticlePointTextureRegion = null;
		}
	}
	
	private void loadFonts() {
		String previousAssetBasePath = FontFactory.getAssetBasePath();
		FontFactory.setAssetBasePath(Constants.FOLDER__FONTS);
		
		if(mMoonHouseFont == null) {
			mMoonHouseFont = FontFactory.createFromAsset(mEngine.getFontManager(), mEngine.getTextureManager(), Constants.FONT_WIDTH, Constants.FONT_HEIGHT, mContext.getAssets(), "moonhouse.ttf",  Constants.FONT_SIZE, true, Color.WHITE_ARGB_PACKED_INT);
			mMoonHouseFont.load();
			mMoonHouseFont.prepareLetters(Constants.DEFAULT_CHARACTERS.toCharArray());
		}
		
		FontFactory.setAssetBasePath(previousAssetBasePath);
	}
	
	private void unloadFonts() {
		if(mMoonHouseFont != null) {
			mMoonHouseFont.unload();
			mMoonHouseFont = null;
		}
	}
	// ======================== Shared Resources ================= //
	
	// ======================== Splash Resources ================= //
	public ITextureRegion mLogoTextureRegion;
	
	public void loadSplashSceneResources() {
		loadSplashSceneTextures();
	}
	
	public void unloadSplashSceneResources() {
		unloadSplashSceneTextures();
	}
	
	private void loadSplashSceneTextures() {
		String previousAssetBasePath = BitmapTextureAtlasTextureRegionFactory.getAssetBasePath();
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(Constants.FOLDER__SPLASH_TEXTURES);
		
		if(mLogoTextureRegion == null) {
			BuildableBitmapTextureAtlas textureAtlas = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 480, 480);
			mLogoTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas, mContext, "logo.png");
			try {
				textureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
				textureAtlas.load();
			} catch (TextureAtlasBuilderException e) {
				
			}
		}
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(previousAssetBasePath);
	}
	
	private void unloadSplashSceneTextures() {
		if(mLogoTextureRegion != null) {
			mLogoTextureRegion.getTexture().unload();
			mLogoTextureRegion = null;
		}
	}
	// ======================== Splash Resources ================= //
	
	// ======================== MainMenu Resources ================= //
	public ITextureRegion mMainMenuBackgroundTextureRegion;
	
	public void loadHomeSceneResources() {
		loadHomeSceneTextures();
	}
	
	public void unloadHomeSceneResources() {
		unloadHomeSceneTextures();
	}
	
	private void loadHomeSceneTextures() {
		String previousAssetBasePath = BitmapTextureAtlasTextureRegionFactory.getAssetBasePath();
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(Constants.FOLDER__HOME_TEXTURES);
		
		if(mMainMenuBackgroundTextureRegion == null) {
			BuildableBitmapTextureAtlas textureAtlas = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 800, 800);
			mMainMenuBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas, mContext, "bg__main_menu.png");
			try {
				textureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
				textureAtlas.load();
			} catch (TextureAtlasBuilderException e) {
				
			}
		}
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(previousAssetBasePath);
	}
	
	private void unloadHomeSceneTextures() {
		if(mMainMenuBackgroundTextureRegion != null) {
			mMainMenuBackgroundTextureRegion.getTexture().unload();
			mMainMenuBackgroundTextureRegion = null;
		}
	}
	// ======================== MainMenu Resources ================= //
	
	// ======================== Game Resources ================= //
	public ITiledTextureRegion mPlayerShipTextureRegion;
	public ITextureRegion mLaserBeamTextureRegion;
	
	public void loadGameSceneResources() {
		loadGameSceneTextures();
	}
	
	public void unloadGameSceneResources() {
		unloadGameSceneTextures();
	}
	
	private void loadGameSceneTextures() {
		String previousAssetBasePath = BitmapTextureAtlasTextureRegionFactory.getAssetBasePath();
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(Constants.FOLDER__GAME_TEXTURES);
		
		if(mPlayerShipTextureRegion == null) {
			BuildableBitmapTextureAtlas textureAtlas = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 116, 128);
			mPlayerShipTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(textureAtlas, mContext, "player_ship.png", 3, 3);
			try {
				textureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
				textureAtlas.load();
			} catch (TextureAtlasBuilderException e) {
				
			}
		}
		
		if(mLaserBeamTextureRegion == null) {
			BuildableBitmapTextureAtlas textureAtlas = new BuildableBitmapTextureAtlas(mEngine.getTextureManager(), 40, 1280);
			mLaserBeamTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(textureAtlas, mContext, "laser_beam.png");
			try {
				textureAtlas.build(new BlackPawnTextureAtlasBuilder<IBitmapTextureAtlasSource, BitmapTextureAtlas>(0, 0, 0));
				textureAtlas.load();
			} catch (TextureAtlasBuilderException e) {
				
			}
		}
		
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath(previousAssetBasePath);
	}
	
	private void unloadGameSceneTextures() {
		if(mPlayerShipTextureRegion != null) {
			mPlayerShipTextureRegion.getTexture().unload();
			mPlayerShipTextureRegion = null;
		}
		
		if(mLaserBeamTextureRegion != null) {
			mLaserBeamTextureRegion.getTexture().unload();
			mLaserBeamTextureRegion = null;
		}
	}
	// ======================== Game Resources ================= //
}
